<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>登录</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="robots" content="all,follow">
    <link rel="stylesheet" href="https://www.jq22.com/jquery/bootstrap-4.2.1.css">
    <link rel="stylesheet" href="/static/css/style.default.css" id="theme-stylesheet">
</head>
<body>
<div class="page login-page" id="app">
    <div class="container d-flex align-items-center">
        <div class="form-holder has-shadow">
            <div class="row">
                <!-- Logo & Information Panel-->
                <div class="col-lg-6">
                    <div class="info d-flex align-items-center">
                        <div class="content">
                            <div class="logo">
                                <h1>欢迎登录</h1>
                            </div>
                            <p>新闻推荐系统</p>
                        </div>
                    </div>
                </div>
                <!-- Form Panel    -->
                <div class="col-lg-6 bg-white">
                    <div class="form d-flex align-items-center">
                        <div class="content">
                            <form method="post" action="login.html" class="form-validate" id="loginFrom">
                                <div class="form-group">
                                    <input id="login-username" type="text" name="userName" required data-msg="请输入用户名"
                                           placeholder="用户名" value="admin" class="input-material" v-model="username">
                                </div>
                                <div class="form-group">
                                    <input id="login-password" type="password" name="passWord" required data-msg="请输入密码"
                                           placeholder="密码" class="input-material"  v-model="password">
                                </div>
                                <button  type="button" class="btn btn-primary" @click="login">登录</button>
                                <div style="margin-top: -40px;">

                                    <div class="custom-control custom-checkbox " style="float: right;">
                                        <input type="checkbox" class="custom-control-input" id="check2">
                                        <label class="custom-control-label" for="check2">自动登录</label>
                                    </div>
                                    <div class="custom-control custom-checkbox " style="float: right;">
                                        <input type="checkbox" class="custom-control-input" id="check1">
                                        <label class="custom-control-label" for="check1">记住密码&nbsp;&nbsp;</label>
                                    </div>
                                </div>
                            </form>
                            <br/>
                            <small style="position: relative;top: 15px;">没有账号?</small>
                            <a href="http://127.0.0.1:8000/register/" style="position: relative;top: 15px;" class="signup">&nbsp;注册</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- JavaScript files-->
<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="https://www.jq22.com/jquery/bootstrap-3.3.4.js"></script>
<script src="/static/vendor/jquery-validation/jquery.validate.min.js"></script><!--表单验证-->
<!-- Main File-->
<script src="/static/js/front.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            username: "",
            password: "",
        },
        methods: {

            // 登录
            login() {
                console.log(this.username);
                if (this.username === "" || this.password === "") {
                    alert("输入框不能为空")
                } else {
                    axios.post('login/auth/', {
                        username: this.username,
                        password: this.password
                    }).then(response => {
                        if (response.data['code'] == 200) {
                            alert("登录成功")
                            sessionStorage.setItem('username', this.username)
                            window.location.href = "http://127.0.0.1:8000/"
                        } else if (response.data['code'] == 444) {
                            alert("账号或者密码错误")
                        }
                    }).catch(error => {
                        console.log(error)
                        alert("请求异常")
                    })
                }
            }
        },
    })
</script>
<script>
    $(function () {
        /*判断上次是否勾选记住密码和自动登录*/
        var check1s = localStorage.getItem("check1");
        var check2s = localStorage.getItem("check2");
        var oldName = localStorage.getItem("userName");
        var oldPass = localStorage.getItem("passWord");
        if (check1s == "true") {
            $("#login-username").val(oldName);
            $("#login-password").val(oldPass);
            $("#check1").prop('checked', true);
        } else {
            $("#login-username").val('');
            $("#login-password").val('');
            $("#check1").prop('checked', false);
        }
        if (check2s == "true") {
            $("#check2").prop('checked', true);
            $("#loginFrom").submit();
            //location="https://www.baidu.com?userName="+oldName+"&passWord="+oldPass;//添加退出当前账号功能
        } else {
            $("#check2").prop('checked', false);
        }

    })
</script>
</body>
</html>